package bookstore.controller;

import bookstore.handle.Msg;
import bookstore.mapper.BookMapper;
import bookstore.model.Book;
import bookstore.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @Autowired
    BookMapper bookMapper;

    //按照页码查询书籍
    @RequestMapping("/selectBookByPage")
    public Object selectBookByPage(Integer page) {
        return bookService.bookList(page);
    }

    //书籍的 like 搜索
    @RequestMapping("/selectBookByName")
    public Object selectByName(String name) {
        return bookService.bookByName(name);
    }

    //根据书籍id来得到书籍
    @RequestMapping("/selectBookById")
    public Object selectById(Integer id) {
        Book book = bookMapper.selectById(id);
        if (book.getId() != null) {
            return new Msg(200, "", book);
        } else {
            return new Msg(0, "没有此书籍");
        }
    }
}
